package com.tjh.springbootapi.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.koala.apiprototype.annotations.IgnoreValidate;
import com.tjh.springbootapi.entity.EasyPoiExport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Create by koala on 2020-04-06
 */

@RestController
@RequestMapping("/easyPoiImport")
public class EasyPoiImportController {

    @IgnoreValidate
    @PostMapping("/excelImport")
    public void importExcel(@RequestParam("file") MultipartFile file) {
        System.out.println(file);
        ImportParams importParams = new ImportParams();
        // 因为Excel是从第2行开始才有正式数据,所以我们这里从第1行开始读取数据
        importParams.setHeadRows(1);

        // 获取数据
        try {
            ExcelImportResult<EasyPoiExport> data = ExcelImportUtil.importExcelMore(file.getInputStream(), EasyPoiExport.class, importParams);
            List<EasyPoiExport> list = data.getList();
            System.out.println(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
